package com.zyh.question.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyh.common.utils.SecurityUtils;
import com.zyh.exam.domain.ExamPaper;
import com.zyh.exam.mapper.ExamPaperMapper;
import com.zyh.exam.service.impl.ExamPaperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zyh.question.mapper.ExamQuestionMapper;
import com.zyh.question.domain.ExamQuestion;
import com.zyh.question.service.IExamQuestionService;

/**
 * 选项表Service业务层处理
 *
 * @author zyh
 * @date 2025-02-20
 */
@Service
public class ExamQuestionServiceImpl extends ServiceImpl<ExamQuestionMapper, ExamQuestion> implements IExamQuestionService {
    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    @Autowired
    private ExamPaperServiceImpl examPaperService;

    /**
     * 查询选项表
     *
     * @param id 选项表主键
     * @return 选项表
     */
    @Override
    public ExamQuestion selectExamQuestionById(Long id) {
        return examQuestionMapper.selectExamQuestionById(id);
    }

    /**
     * 查询选项表列表
     *
     * @param examQuestion 选项表
     * @return 选项表
     */
    @Override
    public List<ExamQuestion> selectExamQuestionList(ExamQuestion examQuestion) {
        List<ExamQuestion> questionList = examQuestionMapper.selectExamQuestionList(examQuestion);
        List<Long> paperIds = questionList.stream().map(ExamQuestion::getPaperId).collect(Collectors.toList());
        for (Long paperId : paperIds) {
            ExamPaper examPaper = examPaperService.selectExamPaperById(paperId);
            for (ExamQuestion question : questionList) {
                if (question.getPaperId().equals(paperId)) {
                    question.setExamPaper(examPaper);
                }
            }
        }
        return questionList;
    }

    /**
     * 新增选项表
     *
     * @param examQuestion 选项表
     * @return 结果
     */
    @Override
    public int insertExamQuestion(ExamQuestion examQuestion) {
        examQuestion.setCreateBy(SecurityUtils.getUsername());
        examQuestion.setCreateTime(new Date());
        examQuestion.setDelFlag("0");
        // 更新试卷题目数量
        int i = examQuestionMapper.insertExamQuestion(examQuestion);
        // 添加数据后更新试卷题目数量
        if (i > 0) {
            List<ExamQuestion> questionList = examQuestionMapper.selectList(
                    new LambdaQueryWrapper<ExamQuestion>()
                            .eq(ExamQuestion::getPaperId, examQuestion.getPaperId()));
            ExamPaper examPaper = examPaperService.getOne(
                    new LambdaQueryWrapper<ExamPaper>()
                            .eq(ExamPaper::getId, examQuestion.getPaperId()));
            examPaper.setQuestionTotal((long) questionList.size());
            examPaperService.updateExamPaper(examPaper);
        }
        return i;
    }

    /**
     * 修改选项表
     *
     * @param examQuestion 选项表
     * @return 结果
     */
    @Override
    public int updateExamQuestion(ExamQuestion examQuestion) {
        examQuestion.setUpdateBy(SecurityUtils.getUsername());
        examQuestion.setUpdateTime(new Date());
        return examQuestionMapper.updateExamQuestion(examQuestion);
    }

    /**
     * 批量删除选项表
     *
     * @param ids 需要删除的选项表主键
     * @return 结果
     */
    @Override
    public int deleteExamQuestionByIds(Long[] ids) {
        int count = 0;
        // 使用一个 Map 来存储每个试卷的题目数量变化
        Map<Long, Integer> paperQuestionCountMap = new HashMap<>();
        for (Long id : ids) {
            // 根据id查到选项
            ExamQuestion examQuestion = examQuestionMapper.selectExamQuestionById(id);
            if (examQuestion != null) {
                Long paperId = examQuestion.getPaperId();
                // 执行逻辑删除
                int i = examQuestionMapper.deleteExamQuestionById(id);
                if (i > 0) {
                    // 删除成功
                    count++;
                    // 记录每个试卷的题目数量变化
                    paperQuestionCountMap.put(paperId, paperQuestionCountMap.getOrDefault(paperId, 0) + 1);
                }
            }
        }
        // 更新每个试卷的题目数量
        for (Map.Entry<Long, Integer> entry : paperQuestionCountMap.entrySet()) {
            Long paperId = entry.getKey();
            Integer questionCount = entry.getValue();
            ExamPaper examPaper = examPaperService.selectExamPaperById(paperId);
            if (examPaper != null) {
                examPaper.setQuestionTotal(examPaper.getQuestionTotal() - questionCount);
                examPaperService.updateExamPaper(examPaper);
            }
        }
        return count;
    }


    /**
     * 删除选项表信息
     *
     * @param id 选项表主键
     * @return 结果
     */
    @Override
    public int deleteExamQuestionById(Long id) {
        return examQuestionMapper.deleteExamQuestionById(id);
    }

    @Override
    public List<ExamQuestion> selectExamQuestionByPaperId(Long paperId) {
        return examQuestionMapper.selectExamQuestionByPaperId(paperId);
    }
}
